package org.gbif.crawler.console;

import org.gbif.crawler.console.action.ApplicationPropertiesModule;
import org.gbif.crawler.service.guice.CrawlerMyBatisServiceModule;
import org.gbif.messaging.guice.CrawlerMessagingModule;
import org.gbif.messaging.guice.MessagingModule;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.inject.struts2.Struts2GuicePluginModule;
import org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter;
import org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter;
import org.apache.struts2.sitemesh.FreemarkerPageFilter;
import org.mybatis.guice.XMLMyBatisModule;

/**
 * A guice module containing wiring used for both test and production.
 *
 * @author tim
 */
public class ConsoleModule extends GuiceServletContextListener {

  private ServletModule sm = new ServletModule() {

    @Override
    protected void configureServlets() {
      bind(StrutsPrepareFilter.class).in(Singleton.class);
      bind(FreemarkerPageFilter.class).in(Singleton.class);
      bind(StrutsExecuteFilter.class).in(Singleton.class);
      filter("/*").through(StrutsPrepareFilter.class);
      filter("/*").through(FreemarkerPageFilter.class);
      filter("/*").through(StrutsExecuteFilter.class);
    }
  };
  private XMLMyBatisModule myBatisModude = new XMLMyBatisModule() {

    @Override
    protected void initialize() {
      setClassPathResource("mybatis-config.xml");
      setEnvironmentId("production");
    }
  };

  @Override
  public Injector getInjector() {
    // TODO: cleanup this into the properties file
    Map<String, String> properties = new HashMap<String, String>(5);
    properties.put("crawler.db.JDBC.driver", "com.mysql.jdbc.Driver");
    properties.put("crawler.db.JDBC.url", "jdbc:mysql://localhost/crawler");
    properties.put("crawler.db.JDBC.username", "root");
    properties.put("crawler.db.JDBC.password", "");
    properties.put("crawler.db.JDBC.autoCommit", "true");
    return Guice.createInjector(new Struts2GuicePluginModule(), sm, new MessagingModule(),
      new CrawlerMessagingModule(), new CrawlerMyBatisServiceModule(properties), new ApplicationPropertiesModule());
  }
}
